package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.MultiImageService;
import com.example.mybatisplus.model.domain.MultiImage;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-02-28
 * @version v1.0
 */
@Controller
@RequestMapping("/multiImage")
public class MultiImageController {

    private final Logger logger = LoggerFactory.getLogger( MultiImageController.class );

    @Autowired
    private MultiImageService multiImageService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        MultiImage  multiImage =  multiImageService.getById(id);
        return JsonResponse.success(multiImage);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        multiImageService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateMultiImage(@PathVariable("id") Long  id,MultiImage  multiImage) throws Exception {
        multiImage.setId(id);
        multiImageService.updateById(multiImage);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建MultiImage
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(MultiImage  multiImage) throws Exception {
        multiImageService.save(multiImage);
        return JsonResponse.success(null);
    }

    /**
     * 描述：根据imageId 查询
     *
     */
    @RequestMapping(value = "/{imageId}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getByImageId(@PathVariable("imageId") Long imageId)throws Exception {

        QueryWrapper<MultiImage> wrapper = new QueryWrapper<>();
        wrapper.eq("image_id",imageId);
        List<MultiImage> list = multiImageService.list(wrapper);
        return JsonResponse.success(list);
    }

    /**
     * 描述：根据imageId删除
     *
     */
    @RequestMapping(value = "/{imageId}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteByImageId(@PathVariable("imageId") Long imageId) throws Exception {
        QueryWrapper<MultiImage> wrapper = new QueryWrapper<>();
        wrapper.eq("image_id",imageId);
        multiImageService.remove(wrapper);
        return JsonResponse.success(null);
    }
}

